﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeakView.service;
using LeakView.view.controls;
using LeakView.common;

namespace LeakView.misc.view.controls
{
    public partial class HistroyDataPanel : UserControl
    {
        public HistroyDataPanel()
        {
            InitializeComponent();
        }

        private void HistroyDataPanel_Load(object sender, EventArgs e)
        {
            //this.multiHeadDataGridView1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            if(this.multiHeadDataGridView1.Columns.Count > 0)
            {
                this.multiHeadDataGridView1.Columns[0].MinimumWidth = 140;
            }
            
            this.comboBox1.SelectedIndex = 0;

            if (this.multiHeadDataGridView1.Columns.Count > 9)
            {
                IniAnyConfig config = AnyConfigs.get("APP");
                string headName = config.get("HeadName").GetOrDefault("上游");
                string tailName = config.get("TailName").GetOrDefault("下游");
                this.treeView1.Nodes[1].Nodes[0].Text = tailName;
                this.treeView1.Nodes[1].Nodes[1].Text = headName;
                this.treeView1.Nodes[2].Nodes[0].Text = tailName;
                this.treeView1.Nodes[2].Nodes[1].Text = headName;
                this.treeView1.Nodes[3].Nodes[0].Text = tailName;
                this.treeView1.Nodes[3].Nodes[1].Text = headName;
            }
        }

        private void LoadData()
        {
            this.multiHeadDataGridView1.Rows.Clear();

            var date = this.historyTimeRanger1.Date;

            var tailYL = MiscModal.me.LoadHistory(MiscType.TailYL, date.Year, date.Month, date.Day);
            var headYL = MiscModal.me.LoadHistory(MiscType.HeadYL, date.Year, date.Month, date.Day);
            // 2020.04.24 修改为LS
            //var tailLL = MiscModal.me.LoadHistory(MiscType.TailLL, date.Year, date.Month, date.Day);
            //var headLL = MiscModal.me.LoadHistory(MiscType.HeadLL, date.Year, date.Month, date.Day);
            var tailLL = MiscModal.me.LoadHistory(MiscType.TailLS, date.Year, date.Month, date.Day);
            var headLL = MiscModal.me.LoadHistory(MiscType.HeadLS, date.Year, date.Month, date.Day);
            var tailWD = MiscModal.me.LoadHistory(MiscType.TailWD, date.Year, date.Month, date.Day);
            var headWD = MiscModal.me.LoadHistory(MiscType.HeadWD, date.Year, date.Month, date.Day);
            _ = 1;

            var start = this.historyTimeRanger1.StartTime;
            var end = this.historyTimeRanger1.EndTime;

            int minutes = 15;
            switch(this.comboBox1.SelectedIndex)
            {
                case 0:
                    break;
                case 1:
                    minutes = 30;
                    break;
                case 2:
                    minutes = 60;
                    break;
                case 3:
                    minutes = 120;
                    break;
                default:
                    minutes = 15;
                    break;
            }

            DateTime time = start;
            while(time <= end)
            {

                var millis = Tools.ConvertDataTimeToLong(time);
                var offset = minutes * 60 * 1000;

                var YL_TAIL = SearchNear(tailYL, millis, offset);
                var YL_HEAD = SearchNear(headYL, millis, offset);
                var YL_DELTA = YL_TAIL - YL_HEAD;

                var LL_TAIL = SearchNear(tailLL, millis, offset);
                var LL_HEAD = SearchNear(headLL, millis, offset);
                var LL_DELTA = LL_TAIL - LL_HEAD;

                var WD_TAIL = SearchNear(tailWD, millis, offset);
                var WD_HEAD = SearchNear(headWD, millis, offset);
                var WD_DELTA = WD_TAIL - WD_HEAD;

                var dgv = this.multiHeadDataGridView1;
                int idx = dgv.Rows.Add();   //添加行
                dgv.Rows[idx].Cells[0].Value = time.ToString("yyyy-MM-dd HH:mm");
                dgv.Rows[idx].Cells[1].Value = YL_TAIL.ToString("F3");
                dgv.Rows[idx].Cells[2].Value = YL_HEAD.ToString("F3");
                dgv.Rows[idx].Cells[3].Value = YL_DELTA.ToString("F3");
                dgv.Rows[idx].Cells[4].Value = LL_TAIL.ToString("F3");
                dgv.Rows[idx].Cells[5].Value = LL_HEAD.ToString("F3");
                dgv.Rows[idx].Cells[6].Value = LL_DELTA.ToString("F3");
                dgv.Rows[idx].Cells[7].Value = WD_TAIL.ToString("F3");
                dgv.Rows[idx].Cells[8].Value = WD_HEAD.ToString("F3");
                dgv.Rows[idx].Cells[9].Value = WD_DELTA.ToString("F3");


                time = time.AddMinutes(minutes);
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if(!this.historyTimeRanger1.IsValid)
            {
                MessageBox.Show("参数错误，起始时间必须小于结束时间！", "错误");
                return;
            }

            using(new LoadingStarter())
            {
                LoadData();
            }
        }

        public static double SearchNear(List<AjaxMiscValue> data, long millis, long offset)
        {

            if(data == null)
            {
                return double.NaN;
            }

            AjaxMiscValue found = null;

            long delta = long.MaxValue;

            foreach (var v in data)
            {

                var d = Math.Abs(v.timestamp - millis);
                if (d > offset)
                    continue;

                if (double.IsNaN(v.value))
                    continue;

                if (d < delta)
                {
                    found = v;
                    delta = d;
                }
                else
                {
                    break;
                }
            }

            if(found == null)
            {
                return double.NaN;
            }
            else
            {
                return found.value;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ExportCSV.export(this.multiHeadDataGridView1);
        }
    }

}
